package cn.bdqn.t354.dao;

import cn.bdqn.t354.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import javax.persistence.criteria.*;
import java.util.List;

@SpringBootTest
@RunWith(SpringRunner.class)
public class UserDaoPageTester {
    @Resource
    UserDao userDao;

    @Test
    public void testFindPage(){
        //构造分页条件
        //页数是从0开始
        Pageable pageable= PageRequest.of(0,2);

        //返回的值就是分页对象
        Page<User> page = userDao.findAll(pageable);
        System.out.println("第几页："+(page.getNumber()+1));
        System.out.println("每页显示多少条："+page.getSize());
        System.out.println("总共多少条："+page.getTotalElements());
        System.out.println("总共多少页："+page.getTotalPages());
        System.out.println("当前页显示的数据："+page.getContent());
    }


    @Test
    public void testFindPage2(){
        //构造分页条件
        //页数是从0开始
        Pageable pageable= PageRequest.of(0,2);

        //构造过滤条件
        Specification<User> specification=new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                //1.获取查询的属性
                Path<Object> usrRoleId = root.get("usrRoleId");
                //2.构造查询条件
                Predicate dajun = cb.equal(usrRoleId, 2l);

                return dajun;
            }
        };
        //返回的值就是分页对象
        Page<User> page = userDao.findAll(specification,pageable);
        System.out.println("第几页："+(page.getNumber()+1));
        System.out.println("每页显示多少条："+page.getSize());
        System.out.println("总共多少条："+page.getTotalElements());
        System.out.println("总共多少页："+page.getTotalPages());
        System.out.println("当前页显示的数据："+page.getContent());
    }

    @Test
    public void testFindPage3(){
        //构造排序条件
        Sort sort=new Sort(Sort.Direction.DESC,"usrId");

        //构造分页条件
        //页数是从0开始
        Pageable pageable= PageRequest.of(0,2,sort);

        //构造过滤条件
        Specification<User> specification=new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                //1.获取查询的属性
                Path<Object> usrRoleId = root.get("usrRoleId");
                //2.构造查询条件
                Predicate dajun = cb.equal(usrRoleId, 2l);

                return dajun;
            }
        };
        //返回的值就是分页对象
        Page<User> page = userDao.findAll(specification,pageable);
        System.out.println("第几页："+(page.getNumber()+1));
        System.out.println("每页显示多少条："+page.getSize());
        System.out.println("总共多少条："+page.getTotalElements());
        System.out.println("总共多少页："+page.getTotalPages());
        System.out.println("当前页显示的数据："+page.getContent());
    }



}
